package logic

import (
	"context"
	"fmt"
	"net/http"
	"work-safety/api/internal/svc"
	"work-safety/api/internal/types"
	"work-safety/pkg"

	"github.com/zeromicro/go-zero/core/logx"
)

type TonLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewTonLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TonLogic {
	return &TonLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *TonLogic) Ton(r *http.Request) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	file, m, err := r.FormFile("image")
	if err != nil {
		return &types.Response{
			Message: "图片未导入",
			Coce:    500,
			Data:    nil,
		}, nil
	}
	fmt.Println(file)
	if m.Size > 5*1024*1240 {
		return &types.Response{
			Message: "图片内存太大",
			Coce:    500,
			Data:    nil,
		}, nil
	}
	jwt, s := pkg.Jwt(m.Filename)
	if s == ".jpg" || s == ".webp" || s == ".svg" {
		return &types.Response{
			Message: "图片格式不对",
			Coce:    500,
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Message: "图片上传成功",
		Coce:    200,
		Data:    jwt,
	}, nil
}
